package fr.afcepf.atod18.fw.parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import fr.afcepf.atod18.fw.action.IAction;
import fr.afcepf.atod18.fw.entities.Action;
import fr.afcepf.atod18.fw.entities.Form;
import fr.afcepf.atod18.fw.exception.FwException;
import fr.afcepf.atod18.fw.form.IActionForm;

public class ParserMapInit {
    
    private static final int ERR_RESSOURCE_NULL = -1;
    
    private Map<String, Class<IAction>> mapAction  = new HashMap<String, Class<IAction>>();
    private Map<String, Class<IActionForm>> mapActionForm  = new HashMap<String, Class<IActionForm>>();
    private Map<String, String> mapActionErr  = new HashMap<String, String>();
    private String nomFicRessources;
    private ParserXml parserXml;


    public ParserMapInit(String fichierResource) throws FwException {
        super();
        parserXml = new ParserXml();
        
        nomFicRessources = Thread.currentThread().getContextClassLoader().getResource("../.."+fichierResource).getPath();
        if (nomFicRessources == null)
            throw new FwException("Fichier resource non trouve", ERR_RESSOURCE_NULL);
    }

    public void initMaps() throws FwException {      
        
        List<Map<String, Object>> listeMapsParsees = null;

        ParserXml parserXml = new ParserXml();
        
        Action a = new Action();
        Form f = new Form();
       
        List<Object> lTags = new ArrayList<Object>();
        lTags.add(a);
        lTags.add(f);
        
        try {
            listeMapsParsees = parserXml.parse(nomFicRessources, lTags);
            System.out.println("listeMapsParsees de taille "+ listeMapsParsees.size());
        } catch (FwException fwe) {
            fwe.printStackTrace();
        } catch(ParserConfigurationException e){
            e.printStackTrace();
    	} catch(SAXException e){
    		e.printStackTrace();
		} catch(IOException e){
			e.printStackTrace();
		}
        for (int i =0; i<listeMapsParsees.size(); i++ ){
            Map<String, Object> map = listeMapsParsees.get(i);
            
            Collection<Object> coll = map.values();
            List<Object> tempList = new ArrayList<Object>(coll);
            
            for (int j = 0; j < tempList.size(); j++){
                Object o = tempList.get(j);
                System.out.println(o.toString());
                if ( o.getClass().getSimpleName().equals("Action")) {
                    /* on remplit la mapAction*/
                    Action action = (Action) o;
                    try {
                        mapAction.put(action.getUrl_pattern(), (Class<IAction>) Class.forName (action.getForm_name()));
                        mapActionErr.put(action.getUrl_pattern(), action.getInput_name());
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (o.getClass().getSimpleName().equals("Form")){
                    /* on remplit la mapActionForm*/
                    Form form = (Form) o;
                    try {
                        mapActionForm.put(form.getForm_name(), (Class<IActionForm>) Class.forName (form.getForm_class()));
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(mapAction.size()+ " actions dans mapAction");
            System.out.println(mapActionForm.size()+ " forms dans mapActionForm");
        }
    }
           
    
    /**
     * @return the mapAction
     */
    public Map<String, Class<IAction>> getMapAction() {
        return mapAction;
    }
    /**
     * @param paramMapAction the mapAction to set
     */
    public void setMapAction(Map<String, Class<IAction>> paramMapAction) {
        mapAction = paramMapAction;
    }
    /**
     * @return the mapActionForm
     */
    public Map<String, Class<IActionForm>> getMapActionForm() {
        return mapActionForm;
    }
    /**
     * @param paramMapActionForm the mapActionForm to set
     */
    public void setMapActionForm(Map<String, Class<IActionForm>> paramMapActionForm) {
        mapActionForm = paramMapActionForm;
    }
    /**
     * @return the mapActionErr
     */
    public Map<String, String> getMapActionErr() {
        return mapActionErr;
    }
    /**
     * @param paramMapActionErr the mapActionErr to set
     */
    public void setMapActionErr(Map<String, String> paramMapActionErr) {
        mapActionErr = paramMapActionErr;
    }
}
